import Vue from 'vue'
import Router from 'vue-router'

// import Login from './components/Login.vue'
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ './components/Login.vue')
// import Home from './components/Home.vue'
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ './components/Home.vue')
// import Welcome from './components/Welcome.vue'
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ './components/Welcome.vue')

// 订单模块
const OrderList = () => import(/* webpackChunkName: "Order" */ './components/order/List.vue')
const OrderSave = () => import(/* webpackChunkName: "Order" */ './components/order/Save.vue')
const OrderUpdate = () => import(/* webpackChunkName: "Order" */ './components/order/Update.vue')
const OrderDetail = () => import(/* webpackChunkName: "Order" */ './components/order/Detail.vue')
const PrintOrder = () => import(/* webpackChunkName: "Order" */ './components/order/Print.vue')

// 采购模板
const PurchaseList = () => import(/* webpackChunkName: "Purchase" */ './components/purchase/List.vue')
const PurchaseUpdate = () => import(/* webpackChunkName: "Purchase" */ './components/purchase/Update.vue')
const PrintPurchase = () => import(/* webpackChunkName: "Purchase" */ './components/purchase/Print.vue')

// 仓库模块
const HouseList = () => import(/* webpackChunkName: "House" */ './components/house/List.vue')
const HouseSave = () => import(/* webpackChunkName: "House" */ './components/house/Save.vue')
const HouseMulti = () => import(/* webpackChunkName: "House" */ './components/house/SaveMulti.vue')
const HouseUpdate = () => import(/* webpackChunkName: "House" */ './components/house/Update.vue')

// 展厅模块
const ShowList = () => import(/* webpackChunkName: "Show" */ './components/show/List.vue')
const PrintShow = () => import(/* webpackChunkName: "Show" */ './components/show/Print.vue')
const ShowSave = () => import(/* webpackChunkName: "Show" */ './components/show/Save.vue')
const ShowUpdate = () => import(/* webpackChunkName: "Show" */ './components/show/Update.vue')
const ShowPurchase = () => import(/* webpackChunkName: "Show" */ './components/show/Purchase.vue')

// 出入库明细
const HouseInOutList = () => import(/* webpackChunkName: "Inout" */ './components/inout/HouseList.vue')
const ShowInOutList = () => import(/* webpackChunkName: "Inout" */ './components/inout/ShowList.vue')

// 下载模块
const DownLoad = () => import(/* webpackChunkName: "DownLoad" */ './components/download/download.vue')

Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', name: 'welcome', component: Welcome },
        { path: '/order-list', name: 'order-list', component: OrderList },
        { path: '/order-save', name: 'order-save', component: OrderSave },
        { path: '/order-update', name: 'order-update', component: OrderUpdate },
        { path: '/order-detail', name: 'order-detail', component: OrderDetail },
        { path: '/purchase-list', name: 'purchase-list', component: PurchaseList },
        { path: '/purchase-update', name: 'purchase-update', component: PurchaseUpdate },
        { path: '/house-list', name: 'house-list', component: HouseList },
        { path: '/house-save', name: 'house-save', component: HouseSave },
        { path: '/house-multi', name: 'house-multi', component: HouseMulti },
        { path: '/house-update', name: 'house-update', component: HouseUpdate },
        { path: '/show-list', name: 'show-list', component: ShowList },
        { path: '/show-save', name: 'show-save', component: ShowSave },
        { path: '/show-update', name: 'show-update', component: ShowUpdate },
        { path: '/show-purchase', name: 'show-purchase', component: ShowPurchase },
        { path: '/house-inout-list', name: 'house-inout-list', component: HouseInOutList },
        { path: '/show-inout-list', name: 'show-inout-list', component: ShowInOutList },
        { path: '/download', name: 'download', component: DownLoad}
      ]
    },
    { path: '/print-purchase', name: 'print-purchase', component: PrintPurchase },
    { path: '/print-order', name: 'print-order', component: PrintOrder },
    { path: '/print-show', name: 'print-show', component: PrintShow }
  ]
})
// 设置登录过期时间（一天）
let EXPIRESTIME = 86400000

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转
  if (to.name !== 'print-purchase' && to.name !== 'print-order' && to.name !== 'print-show') {
    document.querySelector('html').setAttribute('style', 'height: 100%;margin: 0;padding: 0;min-width: 1366px;')
    document.querySelector('body').setAttribute('style', 'height: 100%;margin: 0;padding: 0;min-width: 1366px;')
  } else {
    document.querySelector('html').setAttribute('style', 'height: 0;padding: 0;min-width: 920px;')
    document.querySelector('body').setAttribute('style', 'height: 0;margin: 8px;padding: 0;min-width: 920px;')
  }
  const str = to.name
  if (str && str.indexOf('list') !== -1) {
    localStorage.removeItem('HouseId')
    localStorage.removeItem('PrintOrder')
    localStorage.removeItem('SaveOrder')
    localStorage.removeItem('OrderId')
    localStorage.removeItem('PrintId')
    localStorage.removeItem('PurchaseId')
    localStorage.removeItem('PrintShow')
    localStorage.removeItem('ShowPurchaseId')
    localStorage.removeItem('SaveShow')
    localStorage.removeItem('ShowId')
  }
  const tokenStr = localStorage.getItem('token')
  if (!localStorage.getItem('token') || !localStorage.getItem('hy_access')) {
    if (to.path === '/login') { // 未登录且已经在Login页了，直接放行
      return next()
    }
    return next('/login')
  }
  if (to.path === '/login') {
    return next('/home')
  }
  let date = new Date().getTime()
  // 如果大于就是过期了，如果小于或等于就还没过期
  if (date - localStorage.getItem('time') > EXPIRESTIME) {
    localStorage.removeItem('token')
    localStorage.removeItem('time')
    localStorage.removeItem('role')
    localStorage.removeItem('access')
    next({
      name: 'login'
    })
  }
  next()
})

export default router
